<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>订单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">

    <style>
        .layui-hide{  /*最后的pic为字段的field*/
            height: 100%;
            max-width: 100%;
        }
        .layui-table-cell{
            height:auto !important;
        }
    </style>
</head>
<body>



<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">订单管理</div>
                <div class="layui-card-body">
                    <div class="layui-form-item layui-form">


                        <div class="layui-inline">
                            <label class="layui-form-label">核销码</label>
                            <div class="layui-input-block">
                                <input type="text" name="cancel_code" id="cancel_code" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <div class="layui-btn-group test-table-operate-btn">
                                <button class="layui-btn" data-type="searchUserButton">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>

                    </div>
                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate" lay-size="lg">



                    </table>
                    <script type="text/html" id="test-pass">

                    </script>
                    <script type="text/html" id="test-table-operate-barDemo">

                        {{# if(d.order_status==1){}}
                        <a class="layui-btn layui-btn-green layui-btn-xs" lay-event="push">核销</a>

                        {{# } }}

                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set','layedit','upload'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate,
            upload = layui.upload
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Order/cancel')}"
            ,height: 'full-100'
            ,cols: [[
                {field:'order_id',align:"center",title: 'ID'}
                ,{field:'order_sn',align:"center",title: '订单编号'}
                ,{field:'gtour_title',align:"center",title: '商品名称'}
                ,{field:'linkman',align:"center",title: '取票人姓名'}
                ,{field:'tel',align:"center",title: '取票人手机号'}
                ,{field:'cancel_code',align:"center",title: '核销码'}
                ,{field:'order_price',align:"center",title: '订单价格'}
                ,{field:'pay_type',align:"center",title: '支付方式',templet:function(d){
                        if(d.pay_type == 1){
                            return "支付宝";
                        }else if (d.pay_type == 2){
                            return "微信";
                        }
                    }}
                ,{field:'create_time',align:"center",title: '下订单时间'}
                ,{align:'center', title: '操作', toolbar: '#test-table-operate-barDemo'}

            ]]
            ,page: true
            ,limit: 20
        });

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });
        var search_start = laydate.render({ //活动开始
            elem: '#search_start_time',
            range:'~'

        });
        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;

            if(obj.event === 'push') {
                layer.confirm('核销'+data.order_sn+'订单吗',{
                    btn: ['确认', '取消']
                    ,yes:function(){
                        var loading = layer.load(1, {
                            shade: [0.1,'#fff'] //0.1透明度的白色背景
                        });
                        $.ajax({
                            url: "{:url('Order/cancelPass')}",
                            type: 'post',
                            dataType: 'json',
                            data: {id:data.order_id,sn:data.order_sn,money:data.order_price},
                            success: function(msg){
                                layer.close(loading);
                                if (msg === 0) {
                                    layer.msg('核销成功', {icon: 1, time: 1000},function(){
                                        window.location.reload();
                                    });
                                }else if(msg ===2){
                                    layer.msg('已核销', {icon: 1, time: 1000},function(){
                                        window.location.reload();
                                    });
                                }else if(msg === 3){
                                    layer.msg('已过核销日期',{icon:2,time:1000},function(){
                                        window.location.reload();
                                    });
                                }else if(msg === 4){
                                    layer.msg('非本日门票订单',{icon:2,time:1000},function(){
                                        window.location.reload();
                                    });
                                }else if(msg === 7){
                                    layer.msg('订单已过期',{icon:2,time:1000},function(){
                                        window.location.reload();
                                    });
                                }
                                else{
                                    layer.msg('核销失败',{icon:2,time:1000});
                                }
                            }
                        });
                    }
                    ,btn2:function(){
                        layer.closeAll();
                    }
                });
            }

        });




        var $ = layui.$, active = {
            searchUserButton:function(){
                var cancel_code  = $('#cancel_code').val();

                table.reload('test-table-operate', {
                    where: {
                        cancel_code:cancel_code,
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                //return false;
            },
            excel:function () {
                window.location.href ="{:url('Order/excel')}"
            }
        };
        //绑定点击事件
        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });

    //格式化时间函数
    function Format(datetime,fmt) {
        if (parseInt(datetime)==datetime) {
            if (datetime.length==10) {
                datetime=parseInt(datetime)*1000;
            } else if(datetime.length==13) {
                datetime=parseInt(datetime);
            }
        }
        datetime=new Date(datetime);
        var o = {
            "M+" : datetime.getMonth()+1,                 //月份
            "d+" : datetime.getDate(),                    //日
            "h+" : datetime.getHours(),                   //小时
            "m+" : datetime.getMinutes(),                 //分
            "s+" : datetime.getSeconds(),                 //秒
            "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
            "S"  : datetime.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
</script>

</body>
</html>